package cn.appsys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.BackendUser;
import cn.appsys.service.backenduser.BackendUserService;
import cn.appsys.tools.Constants;

@Controller
@RequestMapping(value="/back")
public class BackendUserController {
	
	@Resource
	private BackendUserService backendUserService;
	/**
	 * 管理员登陆页
	 * @return
	 */
	@RequestMapping(value="/login")
	public String login(){
		return "backlogin";
	}
	/**
	 * 管理员登录验证
	 * @param userCode
	 * @param userPassword
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/dologin")
	public Object dologin(@RequestParam String userCode,@RequestParam String userPassword,
            HttpServletRequest request,HttpSession session){
		BackendUser user = backendUserService.login(userCode, userPassword);
		if (user != null) {
			session.setAttribute(Constants.USER_SESSION, user);
			return "redirect:/back/main";
		}else {
			request.setAttribute("error", "用户名或密码错误");
			return "backlogin";
		}
	}
	/**
	 * 管理员首页
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/main")
	public Object main(HttpSession session){
		if (session.getAttribute(Constants.USER_SESSION) == null) {
			return "redirect:/back/login";
		}
		return "back/main";
	}
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/logout")
	public Object logout(HttpSession session){
		session.removeAttribute(Constants.USER_SESSION);
		return "backlogin";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
